home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / utility / wzun11sr.zip / UNZIP.H < prev    next >
Text File  |  1992-04-09  |  31KB  |  835 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unzip.h
  4.  
  5.   This header file is used by all of the unzip source files.  Its contents
  6.   are divided into six more-or-less separate sections:  OS-dependent includes,
  7.   (mostly) OS-independent defines, typedefs, function prototypes (or "proto-
  8.   types," in the case of non-ANSI compilers), macros, and global-variable
  9.   declarations.
  10.  
  11.   ---------------------------------------------------------------------------*/
  12.  
  13.  
  14.  
  15. /***************************/
  16. /*  OS-Dependent Includes  */
  17. /***************************/
  18.  
  19. #include <stdio.h>       /* this is your standard header for all C compiles */
  20. #include <ctype.h>
  21. #include <errno.h>       /* used in mapped_name() */
  22. #define DECLARE_ERRNO    /* everybody except MSC 6.0 */
  23. #ifdef VMS               /* sigh...you just KNEW someone had to break this.  */
  24. #  include <types.h>     /*  (placed up here instead of in VMS section below */
  25. #  include <stat.h>      /*   because types.h is used in some other headers) */
  26. #else  /* almost everybody */
  27. #  if defined(THINK_C) || defined(MPW) /* for Macs */
  28. #    include <stddef.h>
  29. #  else
  30. #    include <sys/types.h> /* off_t, time_t, dev_t, ... */
  31. #    include <sys/stat.h>  /* Everybody seems to need this. */
  32. #  endif
  33. #endif                   /*   This include file defines
  34.                           *     #define S_IREAD 0x0100  (owner may read)
  35.                           *     #define S_IWRITE 0x0080 (owner may write)
  36.                           *   as used in the creat() standard function.  Must
  37.                           *   be included AFTER sys/types.h for most systems.
  38.                           */
  39.  
  40.  
  41. /*---------------------------------------------------------------------------
  42.     Next, a word from our Unix (mostly) sponsors:
  43.   ---------------------------------------------------------------------------*/
  44.  
  45. #ifdef UNIX
  46. #  ifndef AMIGA
  47. #    include <sys/param.h>      /* conflict with <sys/types.h>, some systems? */
  48. #  endif
  49. #  ifndef BSIZE
  50. #    define BSIZE   DEV_BSIZE   /* assume common for all Unix systems */
  51. #  endif
  52.  
  53. #  ifndef BSD
  54. #    ifndef AMIGA
  55. #      define NO_MKDIR          /* for mapped_name() */
  56. #    endif
  57. #    include <time.h>
  58.      struct tm *gmtime(), *localtime();
  59. #  else   /* BSD */
  60. #    include <sys/time.h>
  61. #    include <sys/timeb.h>
  62. #  endif
  63.  
  64. #else   /* !UNIX */
  65. #  define BSIZE   512           /* disk block size */
  66. #endif
  67.  
  68. #if defined(V7) || defined(BSD)
  69. #  define strchr    index
  70. #  define strrchr   rindex
  71. #endif
  72.  
  73. /*---------------------------------------------------------------------------
  74.     And now, our MS-DOS and OS/2 corner:
  75.   ---------------------------------------------------------------------------*/
  76.  
  77. #ifdef __TURBOC__
  78. #  define DOS_OS2             /* Turbo C under DOS, MSC under DOS or OS2    */
  79. #  include <sys/timeb.h>      /* for structure ftime                        */
  80. #  include <mem.h>            /* for memcpy()                               */
  81. #else                         /* NOT Turbo C...                             */
  82. #  ifdef MSDOS                /*   but still MS-DOS, so we'll assume it's   */
  83. #    ifndef MSC               /*   Microsoft's compiler and fake the ID, if */
  84. #      define MSC             /*   necessary (it is in 5.0; apparently not  */
  85. #    endif                    /*   in 5.1 and 6.0)                          */
  86. #    include <dos.h>          /* _dos_setftime()                            */
  87. #  endif
  88. #  ifdef OS2                  /* stuff for DOS and OS/2 family version */
  89. #    ifndef MSC
  90. #      define MSC
  91. #    endif
  92. #    include <os2.h>          /* DosQFileInfo(), DosSetFileInfo()? */
  93. #  endif
  94. #endif
  95.  
  96. #ifdef MSC                    /* defined for all versions of MSC now         */
  97. #  define DOS_OS2             /* Turbo C under DOS, MSC under DOS or OS/2    */
  98. #  ifndef __STDC__            /* MSC 5.0 and 5.1 aren't truly ANSI-standard, */
  99. #    define __STDC__ 1        /*   but they understand prototypes...so       */
  100. #  endif                      /*   they're close enough for our purposes     */
  101. #  if defined(_MSC_VER) && (_MSC_VER >= 600)      /* new with 5.1 or 6.0 ... */
  102. #    undef DECLARE_ERRNO      /* errno is now a function in a dynamic link   */
  103. #  endif                      /*   library (or something)--incompatible with */
  104. #endif                        /*   the usual "extern int errno" declaration  */
  105.  
  106. #ifdef DOS_OS2                /* defined for both Turbo C, MSC */
  107. #  include <io.h>             /* lseek(), open(), setftime(), dup(), creat() */
  108. #endif
  109.  
  110. /*---------------------------------------------------------------------------
  111.     Followed by some VMS (mostly) stuff:
  112.   ---------------------------------------------------------------------------*/
  113.  
  114. #ifdef VMS
  115. #  include <time.h>             /* the usual non-BSD time functions */
  116. #  include <file.h>             /* same things as fcntl.h has */
  117. #  include <rmsdef.h>           /* RMS error codes */
  118. #  include "fatdef.h"           /* RMS-related things used by VMSmunch */
  119. #  define SET_TIMES       0     /* these 4 defines are arguments to VMSmunch */
  120. #  define GET_RTYPE       1
  121. #  define CHANGE_RTYPE    2
  122. #  define RESTORE_RTYPE   3
  123. #  define UNIX                  /* can share most of same code from now on */
  124. #  define RETURN   return_VMS   /* VMS interprets return codes incorrectly */
  125. #else /* !VMS */
  126. #  define RETURN   return       /* only used in main() */
  127. #  ifdef V7
  128. #    define O_RDONLY  0
  129. #    define O_WRONLY  1
  130. #    define O_RDWR    2
  131. #  else /* !V7 */
  132. #    ifdef MTS
  133. #      include <sys/file.h>     /* MTS uses this instead of fcntl.h */
  134. #    else /* !MTS */
  135. #      ifdef COHERENT           /* Coherent 3.10/Mark Williams C */
  136. #        include <sys/fcntl.h>
  137. #        define SHORT_NAMES
  138. #        define tzset  settz
  139. #      else /* !COHERENT */
  140. #        include <fcntl.h>      /* #define O_BINARY 0x8000 (no CR/LF */
  141. #      endif /* ?COHERENT */    /*   translation), as used in open() */
  142. #    endif /* ?MTS */
  143. #  endif /* ?V7 */
  144. #endif /* ?VMS */
  145.  
  146. /*---------------------------------------------------------------------------
  147.     And some Mac stuff for good measure:
  148.   ---------------------------------------------------------------------------*/
  149.  
  150. #ifdef THINK_C
  151. #  define MACOS
  152. #  define NOTINT16
  153. #  ifndef __STDC__            /* THINK_C isn't truly ANSI-standard, */
  154. #    define __STDC__ 1        /*   but it understands prototypes...so */
  155. #  endif                      /*   it's close enough for our purposes */
  156. #  include <time.h>
  157. #  include <unix.h>
  158. #  include "macstat.h"
  159. #endif
  160. #ifdef MPW                    /* not tested yet - should be easy enough tho */
  161. #  define MACOS
  162. #  define NOTINT16
  163. #  include <time.h>
  164. #  include <fcntl.h>
  165. #  include "macstat.h"
  166. #endif
  167.  
  168. /*---------------------------------------------------------------------------
  169.     And finally, some random extra stuff:
  170.   ---------------------------------------------------------------------------*/
  171.  
  172. #if __STDC__
  173. #  include <stdlib.h>      /* standard library prototypes, malloc(), etc. */
  174. #  include <string.h>      /* defines strcpy, strcmp, memcpy, etc. */
  175. #else
  176.    char *malloc();
  177.    char *strchr(), *strrchr();
  178.    long lseek();
  179. #endif
  180.  
  181. #ifdef SHORT_NAMES         /* Mark Williams C, ...? */
  182. #  define extract_or_test_files    xtr_or_tst_files
  183. #  define extract_or_test_member   xtr_or_tst_member
  184. #endif
  185.  
  186.  
  187.  
  188.  
  189.  
  190. /*************/
  191. /*  Defines  */
  192. /*************/
  193.  
  194. #define INBUFSIZ          BUFSIZ   /* same as stdio uses */
  195. #define DIR_BLKSIZ        64       /* number of directory entries per block
  196.                                     *  (should fit in 4096 bytes, usually) */
  197. /*
  198.  * If <limits.h> exists on most systems, should include that, since it may
  199.  * define some or all of the following:  NAME_MAX, PATH_MAX, _POSIX_NAME_MAX,
  200.  * _POSIX_PATH_MAX.
  201.  */
  202. #define FILNAMSIZ         (1025)
  203. #ifndef PATH_MAX
  204. #  ifdef MAXPATHLEN                /* defined in <sys/param.h> some systems */
  205. #    define PATH_MAX      MAXPATHLEN
  206. #  else
  207. #    if FILENAME_MAX > 255         /* used like PATH_MAX on some systems */
  208. #      define PATH_MAX    FILENAME_MAX
  209. #    else
  210. #      define PATH_MAX    (FILNAMSIZ - 1)
  211. #    endif
  212. #  endif /* ?MAXPATHLEN */
  213. #endif /* !PATH_MAX */
  214.  
  215. #ifdef ZIPINFO
  216. #  define OUTBUFSIZ       BUFSIZ   /* zipinfo needs less than unzip does    */
  217. #else
  218. #  define OUTBUFSIZ       0x2000   /* unImplode needs power of 2, >= 0x2000 */
  219. #endif
  220.  
  221. #define ZSUFX             ".zip"
  222. #define CENTRAL_HDR_SIG   "\120\113\001\002"   /* the infamous "PK" */
  223. #define LOCAL_HDR_SIG     "\120\113\003\004"   /*  signature bytes  */
  224. #define END_CENTRAL_SIG   "\120\113\005\006"
  225.  
  226. #define SKIP              0    /* choice of activities for do_string() */
  227. #define DISPLAY           1
  228. #define FILENAME          2
  229.  
  230. #define DOS_OS2_FAT_      0    /* version_made_by codes (central dir) */
  231. #define AMIGA_            1
  232. #define VMS_              2    /* MAKE SURE THESE ARE NOT DEFINED ON     */
  233. #define UNIX_             3    /* THE RESPECTIVE SYSTEMS!!  (Like, for   */
  234. #define VM_CMS_           4    /* instance, "UNIX":  CFLAGS = -O -DUNIX) */
  235. #define ATARI_            5
  236. #define OS2_HPFS_         6
  237. #define MAC_              7
  238. #define Z_SYSTEM_         8
  239. #define CPM_              9
  240. /* #define TOPS20_   10  (we're going to need this soon...)  */
  241. #define NUM_HOSTS         10   /* index of last system + 1 */
  242.  
  243. #define STORED            0    /* compression methods */
  244. #define SHRUNK            1
  245. #define REDUCED1          2
  246. #define REDUCED2          3
  247. #define REDUCED3          4
  248. #define REDUCED4          5
  249. #define IMPLODED          6
  250. #define NUM_METHODS       7    /* index of last method + 1 */
  251. /* don't forget to update list_files() appropriately if NUM_METHODS changes */
  252.  
  253. #ifndef MACOS
  254. #  define TRUE            1    /* sort of obvious */
  255. #  define FALSE           0
  256. #endif
  257.  
  258. #define UNZIP_VERSION   11     /* compatible with PKUNZIP 1.1 */
  259.  
  260. #ifndef UNZIP_OS               /* not used yet, but will need for Zip  */
  261. #  ifdef UNIX                  /* (could be defined in Makefile or...) */
  262. #    define UNZIP_OS    UNIX_
  263. #  endif
  264. #  ifdef DOS_OS2
  265. #    define UNZIP_OS    DOS_OS2_FAT_
  266. #  endif
  267. #  ifdef VMS
  268. #    define UNZIP_OS    VMS_
  269. #  endif
  270. #  ifdef MTS
  271. #    define UNZIP_OS    UNIX_
  272. #  endif
  273. #  ifdef MACOS
  274. #    define UNZIP_OS    MAC_
  275. #  endif
  276. #  ifndef UNZIP_OS             /* still not defined:  default setting */
  277. #    define UNZIP_OS    UNKNOWN
  278. #  endif
  279. #endif
  280.  
  281. /*---------------------------------------------------------------------------
  282.     Macros for accessing the ULONG header fields.  When NOTINT16 is *not*
  283.     defined, these fields are allocated as arrays of char within the structs.
  284.     This prevents 32-bit compilers from padding the structs so that ULONGs
  285.     start on 4-byte boundaries (this will not work on machines that can ONLY
  286.     access ULONGs if they start on 4-byte boundaries).  If NOTINT16 *is*
  287.     defined, however, the original data are individually copied into working
  288.     structs consisting of UWORDs and ULONGs (which may therefore be padded
  289.     arbitrarily), so the ULONGs are accessed normally.
  290.   ---------------------------------------------------------------------------*/
  291.  
  292. #ifdef NOTINT16
  293. #  define ULONG_(X)   X
  294. #else
  295. #  define ULONG_(X)   (*((ULONG *) (X)))
  296. #endif
  297.  
  298. /*---------------------------------------------------------------------------
  299.     True sizes of the various headers, as defined by Phil Katz--so it is not
  300.     likely that these will ever change.  But if they do, make sure both these
  301.     defines AND the typedefs below get updated accordingly.
  302.   ---------------------------------------------------------------------------*/
  303.  
  304. #define LREC_SIZE     26    /* lengths of local file headers, central */
  305. #define CREC_SIZE     42    /*  directory headers, and the end-of-    */
  306. #define ECREC_SIZE    18    /*  central-dir record, respectively      */
  307.  
  308.  
  309. #define MAX_BITS      13                 /* used in unShrink() */
  310. #define HSIZE         (1 << MAX_BITS)    /* size of global work area */
  311.  
  312. #define LF   10   /* '\n' on ASCII machines.  Must be 10 due to EBCDIC */
  313. #define CR   13   /* '\r' on ASCII machines.  Must be 13 due to EBCDIC */
  314.  
  315. #ifdef EBCDIC
  316. #  define ascii_to_native(c)   ebcdic[(c)]
  317. #endif
  318.  
  319. #ifndef SEEK_SET        /* These should all be declared in stdio.h!  But   */
  320. #  define SEEK_SET  0   /*  since they're not (in many cases), do so here. */
  321. #  define SEEK_CUR  1
  322. #  define SEEK_END  2
  323. #endif
  324.  
  325.  
  326.  
  327.  
  328.  
  329. /**************/
  330. /*  Typedefs  */
  331. /**************/
  332.  
  333. typedef unsigned char    byte;       /* code assumes UNSIGNED bytes */
  334. typedef long             longint;
  335. typedef unsigned short   UWORD;
  336. typedef unsigned long    ULONG;
  337. typedef char             boolean;
  338.  
  339. /*---------------------------------------------------------------------------
  340.     Zipfile layout declarations.  If these headers ever change, make sure the
  341.     ??REC_SIZE defines (above) change with them!
  342.   ---------------------------------------------------------------------------*/
  343.  
  344. #ifdef NOTINT16
  345.  
  346.    typedef byte   local_byte_header[ LREC_SIZE ];
  347. #      define L_VERSION_NEEDED_TO_EXTRACT_0     0
  348. #      define L_VERSION_NEEDED_TO_EXTRACT_1     1
  349. #      define L_GENERAL_PURPOSE_BIT_FLAG        2
  350. #      define L_COMPRESSION_METHOD              4
  351. #      define L_LAST_MOD_FILE_TIME              6
  352. #      define L_LAST_MOD_FILE_DATE              8
  353. #      define L_CRC32                           10
  354. #      define L_COMPRESSED_SIZE                 14
  355. #      define L_UNCOMPRESSED_SIZE               18
  356. #      define L_FILENAME_LENGTH                 22
  357. #      define L_EXTRA_FIELD_LENGTH              24
  358.  
  359.    typedef byte   central_directory_byte_header[ CREC_SIZE ];
  360. #      define C_VERSION_MADE_BY_0               0
  361. #      define C_VERSION_MADE_BY_1               1
  362. #      define C_VERSION_NEEDED_TO_EXTRACT_0     2
  363. #      define C_VERSION_NEEDED_TO_EXTRACT_1     3
  364. #      define C_GENERAL_PURPOSE_BIT_FLAG        4
  365. #      define C_COMPRESSION_METHOD              6
  366. #      define C_LAST_MOD_FILE_TIME              8
  367. #      define C_LAST_MOD_FILE_DATE              10
  368. #      define C_CRC32                           12
  369. #      define C_COMPRESSED_SIZE                 16
  370. #      define C_UNCOMPRESSED_SIZE               20
  371. #      define C_FILENAME_LENGTH                 24
  372. #      define C_EXTRA_FIELD_LENGTH              26
  373. #      define C_FILE_COMMENT_LENGTH             28
  374. #      define C_DISK_NUMBER_START               30
  375. #      define C_INTERNAL_FILE_ATTRIBUTES        32
  376. #      define C_EXTERNAL_FILE_ATTRIBUTES        34
  377. #      define C_RELATIVE_OFFSET_LOCAL_HEADER    38
  378.  
  379.    typedef byte   end_central_byte_record[ ECREC_SIZE+4 ];
  380. /*     define SIGNATURE                         0   space-holder only */
  381. #      define NUMBER_THIS_DISK                  4
  382. #      define NUM_DISK_WITH_START_CENTRAL_DIR   6
  383. #      define NUM_ENTRIES_CENTRL_DIR_THS_DISK   8
  384. #      define TOTAL_ENTRIES_CENTRAL_DIR         10
  385. #      define SIZE_CENTRAL_DIRECTORY            12
  386. #      define OFFSET_START_CENTRAL_DIRECTORY    16
  387. #      define ZIPFILE_COMMENT_LENGTH            20
  388.  
  389.  
  390.    typedef struct local_file_header {                 /* LOCAL */
  391.        byte version_needed_to_extract[2];
  392.        UWORD general_purpose_bit_flag;
  393.        UWORD compression_method;
  394.        UWORD last_mod_file_time;
  395.        UWORD last_mod_file_date;
  396.        ULONG crc32;
  397.        ULONG compressed_size;
  398.        ULONG uncompressed_size;
  399.        UWORD filename_length;
  400.        UWORD extra_field_length;
  401.    } local_file_header;
  402.  
  403.    typedef struct central_directory_file_header {     /* CENTRAL */
  404.        byte version_made_by[2];
  405.        byte version_needed_to_extract[2];
  406.        UWORD general_purpose_bit_flag;
  407.        UWORD compression_method;
  408.        UWORD last_mod_file_time;
  409.        UWORD last_mod_file_date;
  410.        ULONG crc32;
  411.        ULONG compressed_size;
  412.        ULONG uncompressed_size;
  413.        UWORD filename_length;
  414.        UWORD extra_field_length;
  415.        UWORD file_comment_length;
  416.        UWORD disk_number_start;
  417.        UWORD internal_file_attributes;
  418.        ULONG external_file_attributes;
  419.        ULONG relative_offset_local_header;
  420.    } central_directory_file_header;
  421.  
  422.    typedef struct end_central_dir_record {            /* END CENTRAL */
  423.        UWORD number_this_disk;
  424.        UWORD num_disk_with_start_central_dir;
  425.        UWORD num_entries_centrl_dir_ths_disk;
  426.        UWORD total_entries_central_dir;
  427.        ULONG size_central_directory;
  428.        ULONG offset_start_central_directory;
  429.        UWORD zipfile_comment_length;
  430.    } end_central_dir_record;
  431.  
  432.  
  433. #else   /* !NOTINT16:  read data directly into the structure we'll be using */
  434.  
  435.  
  436.    typedef struct local_file_header {                 /* LOCAL */
  437.        byte version_needed_to_extract[2];
  438.        UWORD general_purpose_bit_flag;
  439.        UWORD compression_method;
  440.        UWORD last_mod_file_time;
  441.        UWORD last_mod_file_date;
  442.        byte crc32[4];
  443.        byte compressed_size[4];
  444.        byte uncompressed_size[4];
  445.        UWORD filename_length;
  446.        UWORD extra_field_length;
  447.    } local_file_header;
  448.  
  449.    typedef struct central_directory_file_header {     /* CENTRAL */
  450.        byte version_made_by[2];
  451.        byte version_needed_to_extract[2];
  452.        UWORD general_purpose_bit_flag;
  453.        UWORD compression_method;
  454.        UWORD last_mod_file_time;
  455.        UWORD last_mod_file_date;
  456.        byte crc32[4];
  457.        byte compressed_size[4];
  458.        byte uncompressed_size[4];
  459.        UWORD filename_length;
  460.        UWORD extra_field_length;
  461.        UWORD file_comment_length;
  462.        UWORD disk_number_start;
  463.        UWORD internal_file_attributes;
  464.        byte external_file_attributes[4];
  465.        byte relative_offset_local_header[4];
  466.    } central_directory_file_header;
  467.  
  468.    typedef struct end_central_dir_record {            /* END CENTRAL */
  469.        byte _sig_[4];  /* space-holder only */
  470.        UWORD number_this_disk;
  471.        UWORD num_disk_with_start_central_dir;
  472.        UWORD num_entries_centrl_dir_ths_disk;
  473.        UWORD total_entries_central_dir;
  474.        byte size_central_directory[4];
  475.        byte offset_start_central_directory[4];
  476.        UWORD zipfile_comment_length;
  477.    } end_central_dir_record;
  478.  
  479. #endif  /* !NOTINT16 */
  480.  
  481.  
  482.  
  483.  
  484.  
  485. /*************************/
  486. /*  Function Prototypes  */
  487. /*************************/
  488.  
  489. #ifndef __              /* This amusing little construct was swiped without  */
  490. #  if __STDC__          /*  permission from the fine folks at Cray Research, */
  491. #    define __(X)   X   /*  Inc.  Should probably give them a call and see   */
  492. #  else                 /*  if they mind, but....  Then again, I can't think */
  493. #    define __(X)   ()  /*  of any other way to do this, so maybe it's an    */
  494. #  endif                /*  algorithm?  Whatever, thanks to CRI.  (Note:     */
  495. #endif                  /*  keep interior stuff parenthesized.)              */
  496. /*
  497.  * Toad Hall Note:  Not to worry:  I've seen this somewhere else too,
  498.  * so obviously it's been stolen more than once.
  499.  * That makes it public domain, right?
  500.  */
  501.  
  502. /*---------------------------------------------------------------------------
  503.     Functions in nunzip.c:
  504.   ---------------------------------------------------------------------------*/
  505.  
  506. void   usage                         __( (void) );
  507. int    process_zipfile               __( (void) );
  508. int    find_end_central_dir          __( (void) );
  509. int    process_end_central_dir       __( (void) );
  510. int    list_files                    __( (void) );
  511. int    extract_or_test_files         __( (void) );
  512. int    extract_or_test_member        __( (void) );
  513. int    process_central_file_header   __( (void) );
  514. int    process_local_file_header     __( (void) );
  515.  
  516. /*---------------------------------------------------------------------------
  517.     Functions in file_io.c:
  518.   ---------------------------------------------------------------------------*/
  519.  
  520. int    open_input_file           __( (void) );
  521. int    readbuf                   __( (char *buf, register unsigned size) );
  522. int    create_output_file        __( (void) );
  523. int    FillBitBuffer             __( (register int bits) );
  524. int    ReadByte                  __( (UWORD *x) );
  525. int    FlushOutput               __( (void) );
  526. /*
  527.  * static int   WriteBuffer       __( (int fd, unsigned char *buf, int len) );
  528.  * static int   dos2unix          __( (unsigned char *buf, int len) );
  529.  */
  530. void   set_file_time_and_close   __( (void) );
  531.  
  532. /*---------------------------------------------------------------------------
  533.     Macintosh file_io functions:
  534.   ---------------------------------------------------------------------------*/
  535.  
  536. #ifdef MACOS
  537. void   macfstest                 __( (int vrefnum, int wd) );
  538. /*
  539.  * static int   IsHFSDisk        __( (int wAppVRefNum) );
  540.  */
  541. int    mkdir                     __( (char *path, int mode) );
  542. void   SetMacVol                 __( (char *pch, short wVRefNum) );
  543. #endif
  544.  
  545. /*---------------------------------------------------------------------------
  546.     Uncompression functions (all internal compression routines, enclosed in
  547.     comments below, are prototyped in their respective files and are invisi-
  548.     ble to external functions):
  549.   ---------------------------------------------------------------------------*/
  550.  
  551. void   unImplode                __( (void) );                  /* unimplod.c */
  552. /*
  553.  * static void   ReadLengths     __( (sf_tree *tree) );
  554.  * static void   SortLengths     __( (sf_tree *tree) );
  555.  * static void   GenerateTrees   __( (sf_tree *tree, sf_node *nodes) );
  556.  * static void   LoadTree        __( (sf_tree *tree, int treesize, sf_node *nodes) );
  557.  * static void   LoadTrees       __( (void) );
  558.  * static void   ReadTree        __( (register sf_node *nodes, int *dest) );
  559.  */
  560.  
  561. void   unReduce                 __( (void) );                  /* unreduce.c */
  562. /*
  563.  * static void   LoadFollowers   __( (void) );
  564.  */
  565.  
  566. void   unShrink                 __( (void) );                  /* unshrink.c */
  567. /*
  568.  * static void   partial_clear   __( (void) );
  569.  */
  570.  
  571. /*---------------------------------------------------------------------------
  572.     Functions in match.c, mapname.c, misc.c, etc.:
  573.   ---------------------------------------------------------------------------*/
  574.  
  575. int       match         __( (char *string, char *pattern) );      /* match.c */
  576. /*
  577.  * static BOOLEAN   do_list      __( (register char *string, char *pattern) );
  578.  * static void      list_parse   __( (char **patp, char *lowp, char *highp) );
  579.  * static char      nextch       __( (char **patp) );
  580.  */
  581.  
  582. int       mapped_name   __( (void) );                           /* mapname.c */
  583.  
  584. void      UpdateCRC     __( (register unsigned char *s, register int len) );
  585. int       do_string     __( (unsigned int len, int option) );      /* misc.c */
  586. UWORD     makeword      __( (byte *b) );                           /* misc.c */
  587. ULONG     makelong      __( (byte *sig) );                         /* misc.c */
  588. void      return_VMS    __( (int zip_error) );                     /* misc.c */
  589. #ifdef ZMEM
  590.    char   *memset       __( (register char *buf, register char init, register unsigned int len) );
  591.    char   *memcpy       __( (register char *dst, register char *src, register unsigned int len) );
  592. #endif      /* These guys MUST be ifdef'd because their definition  */
  593.             /*  conflicts with the standard one.  Others (makeword, */
  594.             /*  makelong, return_VMS) don't matter.                 */
  595.  
  596. int    VMSmunch         __( (char *filename, int action, char *extra) );
  597. #ifdef AMIGA
  598. int    utime            __( (char *file, time_t timep[]) );
  599. #endif
  600.  
  601.  
  602.  
  603.  
  604.  
  605. /************/
  606. /*  Macros  */
  607. /************/
  608.  
  609. #ifndef min    /* MSC defines this in stdlib.h */
  610. #  define min(a,b)   ((a) < (b) ? (a) : (b))
  611. #endif
  612.  
  613.  
  614. #define LSEEK(abs_offset) {longint request=(abs_offset), inbuf_offset=request%INBUFSIZ, bufstart=request-inbuf_offset;\
  615.    if(bufstart!=cur_zipfile_bufstart) {cur_zipfile_bufstart=lseek(zipfd,bufstart,SEEK_SET);\
  616.    if((incnt=read(zipfd,inbuf,INBUFSIZ))<=0) return(51); inptr=inbuf+inbuf_offset; incnt-=inbuf_offset;\
  617.    }else {incnt+=(inptr-inbuf)-inbuf_offset; inptr=inbuf+inbuf_offset; }}
  618.  
  619. /*
  620.  *  Seek to the block boundary of the block which includes abs_offset,
  621.  *  then read block into input buffer and set pointers appropriately.
  622.  *  If block is already in the buffer, just set the pointers.  This macro
  623.  *  is used by process_end_central_dir (unzip.c) and do_string (misc.c).
  624.  *  A slightly modified version is embedded within extract_or_test_files
  625.  *  (unzip.c).  ReadByte and readbuf (file_io.c) are compatible.
  626.  *
  627.  *  macro LSEEK( abs_offset )
  628.  *    {
  629.  *      longint   request = abs_offset;
  630.  *      longint   inbuf_offset = request % INBUFSIZ;
  631.  *      longint   bufstart = request - inbuf_offset;
  632.  *
  633.  *      if (bufstart != cur_zipfile_bufstart) {
  634.  *          cur_zipfile_bufstart = lseek(zipfd, bufstart, SEEK_SET);
  635.  *          if ((incnt = read(zipfd,inbuf,INBUFSIZ)) <= 0)
  636.  *              return(51);
  637.  *          inptr = inbuf + inbuf_offset;
  638.  *          incnt -= inbuf_offset;
  639.  *      } else {
  640.  *          incnt += (inptr-inbuf) - inbuf_offset;
  641.  *          inptr = inbuf + inbuf_offset;
  642.  *      }
  643.  *    }
  644.  *
  645.  */
  646.  
  647.  
  648. #define OUTB(intc) { *outptr++=intc; if (++outcnt==OUTBUFSIZ) FlushOutput(); }
  649.  
  650. /*
  651.  *  macro OUTB(intc)
  652.  *  {
  653.  *      *outptr++=intc;
  654.  *      if (++outcnt==OUTBUFSIZ)
  655.  *          FlushOutput();
  656.  *  }
  657.  *
  658.  */
  659.  
  660.  
  661. #define READBIT(nbits,zdest) { if (nbits <= bits_left) { zdest = (int)(bitbuf & mask_bits[nbits]); bitbuf >>= nbits; bits_left -= nbits; } else zdest = FillBitBuffer(nbits);}
  662.  
  663. /*
  664.  * macro READBIT(nbits,zdest)
  665.  *  {
  666.  *      if (nbits <= bits_left) {
  667.  *          zdest = (int)(bitbuf & mask_bits[nbits]);
  668.  *          bitbuf >>= nbits;
  669.  *          bits_left -= nbits;
  670.  *      } else
  671.  *          zdest = FillBitBuffer(nbits);
  672.  *  }
  673.  *
  674.  */
  675.  
  676.  
  677. #define NUKE_CRs(buf,len) {register int i,j; for (i=j=0; j<len; (buf)[i++]=(buf)[j++]) if ((buf)[j]=='\r') ++j; len=i;}
  678.  
  679. /*
  680.  *  Remove all the ASCII carriage returns from buffer buf (length len),
  681.  *  shortening as necessary (note that len gets modified in the process,
  682.  *  so it CANNOT be an expression).  This macro is intended to be used
  683.  *  BEFORE A_TO_N(); hence the check for CR instead of '\r'.  NOTE:  The
  684.  *  if-test gets performed one time too many, but it doesn't matter.
  685.  *
  686.  *  macro NUKE_CRs( buf, len )
  687.  *    {
  688.  *      register int   i, j;
  689.  *
  690.  *      for ( i = j = 0  ;  j < len  ;  (buf)[i++] = (buf)[j++] )
  691.  *        if ( (buf)[j] == CR )
  692.  *          ++j;
  693.  *      len = i;
  694.  *    }
  695.  *
  696.  */
  697.  
  698.  
  699. #define TOLOWER(str1,str2) {char *ps1,*ps2; ps1=(str1)-1; ps2=(str2); while(*++ps1) *ps2++=(isupper(*ps1))?tolower(*ps1):*ps1; *ps2='\0';}
  700.  
  701. /*
  702.  *  Copy the zero-terminated string in str1 into str2, converting any
  703.  *  uppercase letters to lowercase as we go.  str2 gets zero-terminated
  704.  *  as well, of course.  str1 and str2 may be the same character array.
  705.  *
  706.  *  macro TOLOWER( str1, str2 )
  707.  *    {
  708.  *      register char   *ps1, *ps2;
  709.  *
  710.  *      ps1 = (str1) - 1;
  711.  *      ps2 = (str2);
  712.  *      while ( *++ps1 )
  713.  *        *ps2++ = (isupper(*ps1)) ?  tolower(*ps1)  :  *ps1;
  714.  *      *ps2='\0';
  715.  *    }
  716.  *
  717.  *  NOTES:  This macro makes no assumptions about the characteristics of
  718.  *    the tolower() function or macro (beyond its existence), nor does it
  719.  *    make assumptions about the structure of the character set (i.e., it
  720.  *    should work on EBCDIC machines, too).  The fact that either or both
  721.  *    of isupper() and tolower() may be macros has been taken into account;
  722.  *    watch out for "side effects" (in the C sense) when modifying this
  723.  *    macro.
  724.  */
  725.  
  726.  
  727. #ifndef ascii_to_native
  728.  
  729. #  define ascii_to_native(c)   (c)
  730. #  define A_TO_N(str1)
  731.  
  732. #else
  733.  
  734. #  define NATIVE   /* Used in main() for '-a' and '-c'. */
  735. #  define A_TO_N(str1) { register unsigned char *ps1; for (ps1 = str1; *ps1; ps1++) *ps1 = (ascii_to_native(*ps1)); }
  736.  
  737. /*
  738.  *   Translate the zero-terminated string in str1 from ASCII to the native
  739.  *   character set. The translation is performed in-place and uses the
  740.  *   ascii_to_native macro to translate each character.
  741.  *
  742.  *   macro A_TO_N( str1 )
  743.  *     {
  744.  *     register unsigned char *ps1;
  745.  *
  746.  *     for ( ps1 = str1; *ps1; ps1++ )
  747.  *       *ps1 = ( ascii_to_native( *ps1 ) );
  748.  *     }
  749.  *
  750.  *   NOTE: Using the ascii_to_native macro means that is it the only part of
  751.  *     unzip which knows which translation table (if any) is actually in use
  752.  *     to produce the native character set. This makes adding new character
  753.  *     set translation tables easy insofar as all that is needed is an
  754.  *     appropriate ascii_to_native macro definition and the translation
  755.  *     table itself. Currently, the only non-ASCII native character set
  756.  *     implemented is EBCDIC but this may not always be so.
  757.  */
  758.  
  759. #endif
  760.  
  761.  
  762.  
  763.  
  764.  
  765. /*************/
  766. /*  Globals  */
  767. /*************/
  768.  
  769.    extern int       tflag;
  770. /* extern int       vflag;    (only used in unzip.c)  */
  771.    extern int       cflag;
  772.    extern int       aflag;
  773.    extern int       dflag;
  774.    extern int       Uflag;
  775.    extern int       V_flag;
  776. #ifdef MACOS
  777.    extern int       hfsflag;
  778. #endif
  779.    extern int       lcflag;
  780.    extern unsigned  f_attr;
  781.    extern longint   csize;
  782.    extern longint   ucsize;
  783.  
  784.    extern short     prefix_of[];
  785. #ifdef MACOS
  786.    extern byte      *suffix_of;
  787.    extern byte      *stack;
  788. #else
  789.    extern byte      suffix_of[];
  790.    extern byte      stack[];
  791. #endif
  792.    extern ULONG     crc32val;
  793.    extern UWORD     mask_bits[];
  794.  
  795.    extern byte      *inbuf;
  796.    extern byte      *inptr;
  797.    extern int       incnt;
  798.    extern UWORD     bitbuf;
  799.    extern int       bits_left;
  800.    extern boolean   zipeof;
  801.    extern int       zipfd;
  802. #ifdef MSWIN
  803.    extern char      *zipfn;
  804. #else
  805.    extern char      zipfn[];
  806. #endif
  807.    extern local_file_header   lrec;
  808.    extern struct stat         statbuf;
  809.    extern longint   cur_zipfile_bufstart;
  810.  
  811.    extern byte      *outbuf;
  812.    extern byte      *outptr;
  813. #ifdef MSWIN
  814.    extern byte _far *outout;
  815. #else
  816.    extern byte      *outout;
  817. #endif
  818.    extern longint   outpos;
  819.    extern int       outcnt;
  820.    extern int       outfd;
  821. #ifdef MSWIN
  822.    extern char      *filename;
  823. #else
  824.    extern char      filename[];
  825. #endif
  826.  
  827. #ifdef DECLARE_ERRNO
  828.    extern int       errno;
  829. #endif
  830.  
  831. #ifdef EBCDIC
  832.    extern byte      ebcdic[];
  833. #endif
  834. 
  835.